//
//  BaseDataTool.m
//
//  Created by Northwan on 15/4/14.
//  Copyright (c) 2015年  All rights reserved.
//

#import "BaseDataTool.h"
#import "BaseHttpTool.h"
#import "BaseRequest.h"
#import "YSLodingView.h"
@implementation BaseDataTool

+ (void)getWithUrl:(NSString *)url param:(NSDictionary *)param hasT:(BOOL)hasT resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    if(!param){
        param = [[BaseRequest param] keyValues];
    }
//    JGProgressHUD *hud = [DialogUtils show];
    NSDictionary *mParams = [SignUtils getParams:param shouldLogin:hasT];
    [BaseHttpTool get:url params:mParams success:^(id responseObj) {
//        [hud dismiss];
        if (success) {
            NSLog(@"%@:%@",resultClass,[[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding]);
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
//        [hud dismiss];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithUrl:(NSString *)url param:(NSDictionary *)param hasT:(BOOL)hasT resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    if(!param){
        param = [[BaseRequest param] keyValues];
    }
//    JGProgressHUD *hud = [DialogUtils show];
    NSDictionary *mParams = [SignUtils getParams:param shouldLogin:hasT];
    [BaseHttpTool post:url params:mParams success:^(id responseObj) {
//        [hud dismiss];
        if (success) {
            NSLog(@"服务器返回%@:%@",resultClass,[[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding]);
            
            id result = [resultClass objectWithKeyValues:responseObj];
            [self checkErrorInfo:result];//判断登陆是否过期
            success(result);
        }
    } failure:^(NSError *error) {
//          [hud dismiss];
        if (failure) {
            failure(error);
        }
    }];
}


+(void)checkErrorInfo:(id)result{
    BaseResponse *response = (BaseResponse *)result;
    if(response.data){
        switch (response.code) {
            case 1001:
            case 1002://身份已过期, 密钥不匹配,强制退出
                [[NSNotificationCenter defaultCenter] postNotificationName:kLoginTimeOutNortif object:self];
                break;
          
            default:
                
                break;
        }
    }
}

+ (void)getWithUrl:(NSString *)url param:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
//     JGProgressHUD *hud = [DialogUtils show];
    [BaseHttpTool get:url params:param success:^(id responseObj) {
//        [hud dismiss];
        if (success) {
            NSLog(@"%@:%@",resultClass,[[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding]);
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
//        [hud dismiss];
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postWithUrl:(NSString *)url param:(NSDictionary *)param resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
//    JGProgressHUD *hud = [DialogUtils show];
    [BaseHttpTool post:url params:param success:^(id responseObj) {
//        [hud dismiss];
        if (success) {
            NSLog(@"%@:%@",resultClass,[[NSString alloc]initWithData:responseObj encoding:NSUTF8StringEncoding]);
            id result = [resultClass objectWithKeyValues:responseObj];
            success(result);
        }
    } failure:^(NSError *error) {
//        [hud dismiss];
        if (failure) {
            failure(error);
        }
    }];
}
@end
